<template>
  <div>
    <edu-dialog :title="$t('auditStandard.addCourseGroup')" size="small" :visible.sync="show" :autoHeight="false"
    :confirmBtnText="$t('common.btn.confirm')" :close-on-click-modal="false" @close="close" @confirm="confirm('addCourseGroupForm')">
    <el-form :model="formInfo" ref="addCourseGroupForm" :rules="rules" label-width="130px" label-position="right"
      size="small">
      <el-row>
        <el-col :span="20">
          <el-form-item :label="$t('auditStandard.courseGroupName') + ':'" prop="groupName">
            <el-input v-model="formInfo.groupName" :placeholder="$t('common.pla.pleaseInput')" maxlength="100" style="width: 100%;"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="20">
          <el-form-item :label="$t('auditStandard.courseGroupCredits') + ':'" prop="credits">
            <el-input v-model.number="formInfo.credits" :placeholder="$t('common.pla.pleaseInput')" maxlength="100" style="width: 100%;"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="20" v-if="condition.type == 1">
          <el-form-item label="例外专业码:" prop="exceptionSpecialtyCode">
            <el-input v-model="formInfo.exceptionSpecialtyCode" placeholder="请输入专业代码, 多条请用英文逗号分隔" style="width: 100%;"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    </edu-dialog>
  </div>
</template>
<script>
  import {addCourseGroup} from 'common/src/api/graduationManagement/interStudentManage';
  export default {
    props: {
      show: {
        type: Boolean,
        default: false
      },
      condition: {
        type: Object,
        default: {}
      }
    },
    data() {
      return {
        formInfo: {
          groupName: '',
          credits: '',
          exceptionSpecialtyCode: ''
        },
        rules: {
          groupName: [{
            required: true,
            message: this.$t('auditStandard.validCourseGroupName')
          }],
          credits: [{
            required: true,
            message: this.$t('auditStandard.validCourseGroupCredits')
          }],
          exceptionSpecialtyCode: [
                { 
                    validator: (rule, value, callback) => {
                        let reg= /[\u4e00-\u9fa5]/;
                        if (value && reg.test(value)) {
                            callback(new Error('请输入专业代码, 多条请用英文逗号分隔'));
                        }else {
                            callback();
                        }
                    }, 
                    trigger: 'blur'
                }
              
          ]
        }
      }
    },
    methods: {
      confirm(name) {
        this.$refs[name].validate((valid) => {
          if (valid) {
            this.ok();
          } else {
            return false;
          }
        });
      },
      close() {
        this.$emit('update:show', false);
      },
      ok() {
        let params = {
          ...this.condition,
          ...this.formInfo
        }
        if (this.condition.type == 0) {
            delete params.exceptionSpecialtyCode;
        }
        params.credits =  params.credits + '';
        addCourseGroup(params).then(res =>{
          if (res && res.code === 200) {
            this.$message({
              message: this.$t('auditStandard.addCourseGroupSuccessTip'),
              type: 'success'
            });
            this.$parent.getStandard && this.$parent.getStandard();
            this.$emit('update:show', false);
          }
        }).catch((error)=>{
          if (error && error.message) {
            this.$message({
              message: error.message,
              type: 'error'
            });
          }
        })
      }
    }
  }


</script>